package com.cyandpos.pad.offline.model;

import android.content.ContentValues;
import android.database.Cursor;

import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.offline.offlinedb.CursorUtil;
import com.cyandpos.pad.offline.offlinedb.Sqldb;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.OrderNoData;
import com.cyandpos.pad.posdata.Person;
import com.cyandpos.pad.posdata.PreparePayData;
import com.cyandpos.pad.posdata.SubTicketItem;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.ComSharedPreferences;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.log.MessageLog;
import com.google.gson.Gson;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OfflineSale {
    /**
     * 创建订单
     * @param sqldb
     * @param params
     * @param callback
     */
    public static void createOder(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        int personId=0;
        try {
            Cursor cursor = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.RYDM=" + "'"+params.get("operatorCode") + "'"+"  and (B.FDBH=" + "'"+params.get("storeCode") + "'" + " or B.FDBH=-1) and R.STATUS=1");
            MessageLog.writeLog("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.RYDM=" + params.get("operatorCode") + "  and (B.FDBH=" + params.get("storeCode") + "" + " or B.FDBH=-1) and R.STATUS=1");
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    personId = CursorUtil.getInt(cursor, "PERSON_ID");
                }
                cursor.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        ContentValues values=new ContentValues();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String orderNo= CommonUtils.addZero(Integer.parseInt(params.get("sktno")+""),6) + simpleDateFormat.format(date) + "0" + CommonUtils.addZero(CursorUtil.getNextval(),6);

        values.put("ORDERNO",orderNo);
        values.put("FDBH",params.get("storeCode")+"");
        values.put("SKTNO",params.get("sktno")+"");
        values.put("DDLY",params.get("ddly")+"");
        values.put("DDGN",params.get("orderFunction")+"");
        values.put("JYLX",params.get("ddlx")+"");
        values.put("STATUS","0");
        values.put("KSSJ",simpleDateFormat2.format(date));
        values.put("SMG_HYID","");
        values.put("SKY",personId);
        values.put("OLDORDERNO","");
        values.put("SKTNO_OLD","");
        values.put("JLBH_OLD",0);
        values.put("BJ_OFFLINE",1);
        boolean isInsert=sqldb.insert("POS_ORDER",values);
        MessageLog.writeLog("创建订单POS_ORDER/"+isInsert+"/values="+new Gson().toJson(values));
        if(isInsert){
            OrderNoData orderNoData=new OrderNoData();
            orderNoData.setOrderNo(orderNo);
            callback.onSuccess(CursorUtil.getOfflineBean(orderNoData));
        }else{
            callback.onFailure("订单创建失败!");
        }
    }

    /**
     * 直接保存营业员
     */
    public static void savePerson(Sqldb sqldb, String orderNo,String cashierId,String cashierDeptId){
        ContentValues values=new ContentValues();
        values.put("ORDERNO",orderNo);
        values.put("SKTNO", ErpCommonData.sktno);
        values.put("TCKT_INX",0);
        values.put("YYY",cashierId);
        values.put("DEPTID",cashierDeptId);
        boolean isInsert=sqldb.insert("ORDER_PERSON",values);
    }
    /**
     * 查营业员
     * @param sqldb
     * @param params
     * @param callback
     */
    public static  void QueryPerson(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        try {
            Cursor cursor = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.WORK_TYPE=2 and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1 AND R.RYDM=" + "'"+params.get("personCode") + "'"+"  and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1");
            MessageLog.writeLog("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.WORK_TYPE=2 and (B.FDBH=" + params.get("storeCode") + " or B.FDBH=-1) and R.STATUS=1 AND R.RYDM=" + params.get("personCode") + "  and (B.FDBH=" + params.get("storeCode") + " or B.FDBH=-1) and R.STATUS=1");
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    Person person = new Person();
                    person.setPersonId(CursorUtil.getInt(cursor, "PERSON_ID"));
                    person.setPersonName(CursorUtil.getString(cursor, "PERSON_NAME"));
                    person.setPersonCode(CursorUtil.getString(cursor, "RYDM"));
                    person.setPersonDeptId(CursorUtil.getInt(cursor, "DEPTID"));
                    person.setPersonDeptCode(CursorUtil.getString(cursor, "BMDM"));
                    person.setPersonDeptName(CursorUtil.getString(cursor, "DEPT_NAME"));
                    person.setShopId(CursorUtil.getInt(cursor, "SHOPID"));
                    ContentValues values=new ContentValues();
                    values.put("ORDERNO",params.get("orderNo")+"");
                    values.put("SKTNO", ErpCommonData.sktno);
                    values.put("TCKT_INX",0);
                    values.put("YYY",CursorUtil.getInt(cursor, "PERSON_ID"));
                    values.put("DEPTID",CursorUtil.getInt(cursor, "DEPTID"));
                    boolean isInsert=sqldb.insert("ORDER_PERSON",values);
                    MessageLog.writeLog("插入ORDER_PERSON表"+isInsert+"values="+new Gson().toJson(values));
                    callback.onSuccess(CursorUtil.getOfflineBean(person));
                }
                cursor.close();
            } else {
                callback.onFailure("没有查到营业员!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 查商品
     * @param sqldb
     * @param params
     * @param callback
     */
    public static  void QueryGoods(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        String goodsGuid=params.get("goodsGuid")+"";
        if(goodsGuid!=null&&!"".equals(goodsGuid)&&!"null".equals(goodsGuid)){
            Goods goods=getGoodsFromGuid(sqldb,params);
            if(goods!=null) {
                callback.onSuccess(CursorUtil.getOfflineBean(goods));
            }else{
                callback.onFailure("没有查到此商品!");
            }
        }else {
            getGoodsFromCode(sqldb, params, callback);
        }
    }

    /**
     * 准备付款
     * @param sqldb
     * @param params
     * @param callback
     */
    public static void PreparePay(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        BigDecimal count=new BigDecimal(0);
        BigDecimal price=new BigDecimal(0);
        BigDecimal discount=new BigDecimal(0);

        List<Goods> list=getGoodsList(sqldb,params);
        for(Goods goods :list){
            count = count.add(new BigDecimal(goods.getSaleCount()));
            price = price.add(new BigDecimal(goods.getSaleMoney()));
            discount=discount.add(new BigDecimal(goods.getDiscount()));
        }
        PreparePayData preparePayData=new PreparePayData();
        preparePayData.setGoodsList(list);
        preparePayData.setJlbh(CursorUtil.getJLBH()+"");
        preparePayData.setTotalDiscount(discount+"");
        preparePayData.setTotalCount(count.intValue()+"");
        preparePayData.setTotalAmount(price.subtract(discount)+"");
        preparePayData.setDecMoney("0.0");
        preparePayData.setDecPreferentialMoney("0.0");
        callback.onSuccess(CursorUtil.getOfflineBean(preparePayData));
        ContentValues values = new ContentValues();
        values.put("STATUS","2");
        boolean isUpdate=sqldb.update("POS_ORDER", values, "ORDERNO=?", new String[]{params.get("orderNo") + ""});
        MessageLog.writeLog("准备付款更新POS_ORDER "+isUpdate+"values="+new Gson().toJson(values));
    }



    public static  boolean insertGoodOrder(Sqldb sqldb, Map<String, Object> params, Goods goods){
        ContentValues values = new ContentValues();
        String orderNo=params.get("orderNo").toString();
        values.put("ORDERNO",  orderNo+"");
        values.put("SKTNO", params.get("sktno") + "");
        values.put("TCKT_INX", Integer.parseInt(goods.getTicktInx()));
        values.put("INX", Integer.parseInt(goods.getTicktGoodsInx()));
        values.put("DEPTID", goods.getDeptId());
        values.put("DEPTCODE", goods.getDeptCode());
        values.put("SP_ID", Integer.parseInt(goods.getId()));
        values.put("BARCODE", goods.getBarCode());
        values.put("LSDJ", goods.getPrice());
        values.put("XSSL", goods.getSaleCount());
        values.put("XSJE", new BigDecimal(goods.getPrice()).multiply(new BigDecimal(goods.getSaleCount()))+"");
        values.put("ZKJE", goods.getDiscount());
        values.put("GUID", goods.getGoodsGuid());
        values.put("INPUTCODE", goods.getInputCode());
        values.put("CODE", goods.getCode());
        Cursor cursor = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.WORK_TYPE=2 and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1 AND R.RYDM=" + "'"+params.get("personCode") +"'"+ "  and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1");
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                values.put("YYY",CursorUtil.getInt(cursor, "PERSON_ID"));
            }
        }
        boolean isInsert = sqldb.insert("ORDER_ITEM", values);
        MessageLog.writeLog("插入ORDER_ITEM表"+isInsert+"/values="+new Gson().toJson(values));
        return isInsert;
    }


    /**
     * 查询订单商品
     * @param sqldb
     * @param params
     * @return
     */
    public static List<Goods> getGoodsList(Sqldb sqldb, Map<String, Object> params) {
        List<Goods> goodsList=new ArrayList<>();
        try {
            //ORDERNO ,A.TCKT_INX,A.INX,A.DEPTID,A.SP_ID,A.BARCODE,A.LSDJ,A.XSSL,A.XSJE,A.ZKJE,A.CRMINX,A.GUID,A.INPUTCODE,A.CODE,A.DEPTCODE,A.MBYH,A.YHJE,A.STAMP,A.YYY,A.TCKT_INX_OLD,A.INX_OLD,A.JF,A.BZ,A.CXSPBJ,A.BGH,A.XSDD_JLBH,A.SPJX,A.ORDERNO_ORIGINAL,A.FDBH_ORIGINAL,B.SPTYPE
            Cursor cursor1 = sqldb.find("select * from ORDER_ITEM where ORDERNO="+"'"+params.get("orderNo")+"'");
            MessageLog.writeLog("select * from ORDER_ITEM where ORDERNO="+params.get("orderNo"));
            if (cursor1 != null && cursor1.getCount() > 0) {
                while (cursor1.moveToNext()) {
                        Goods goods = new Goods();
                        goods.setInputCode(CursorUtil.getString(cursor1, "INPUTCODE"));
                        goods.setTicktInx(CursorUtil.getInt(cursor1, "TCKT_INX") + "");
                        goods.setTicktGoodsInx(CursorUtil.getInt(cursor1, "INX") + "");
                        goods.setDeptId(CursorUtil.getInt(cursor1, "DEPTID") + "");
                        goods.setDeptCode(CursorUtil.getString(cursor1, "DEPTCODE"));
                        goods.setId(CursorUtil.getInt(cursor1, "SP_ID") + "");
                        goods.setBarCode(CursorUtil.getString(cursor1, "BARCODE"));
                        goods.setPrice((CursorUtil.getDouble(cursor1, "LSDJ")) + "");
                        goods.setSaleCount(CursorUtil.getInt(cursor1, "XSSL") + "");
                        goods.setSaleMoney((CursorUtil.getDouble(cursor1, "XSJE")) + "");
                        goods.setDiscount((CursorUtil.getDouble(cursor1, "ZKJE")) + "");
                        goods.setGoodsGuid(CursorUtil.getString(cursor1, "GUID"));
                        goods.setCode(CursorUtil.getString(cursor1, "CODE"));
                        goods.setPreferentialMoney((CursorUtil.getDouble(cursor1, "YHJE") - CursorUtil.getDouble(cursor1, "MBYH")) + "");
                        goods.setPersonId(CursorUtil.getInt(cursor1, "YYY") + "");
                        int SP_ID = CursorUtil.getInt(cursor1, "SP_ID");
                        Cursor cursor2 = sqldb.find("select SP_ID,SPCODE,BARCODE,NAME,UNIT,SPGG,PACKED,szspbj,FKCXSBJ,SPTYPE,HSFS,LSDJ,HYLSDJ,STATUS,SPFL,SHOPID,SLYXS,FKCXSSJ from SPXX  where SP_ID=" + SP_ID + " and FDBH=" +"'"+ params.get("storeCode")+"'");
                        if (cursor2 != null && cursor2.getCount() > 0) {
                            while (cursor2.moveToNext()) {
                                goods.setName(CursorUtil.getString(cursor2, "NAME"));
                                goods.setUnit(CursorUtil.getString(cursor2, "UNIT"));
                                goods.setBulkType(CursorUtil.getInt(cursor2, "SPTYPE") + "");
                                goods.setClassType(CursorUtil.getString(cursor2, "SPTYPE"));
                                goods.setCanSaleCount(CursorUtil.getInt(cursor2, "FKCXSSJ") + "");
                            }
                        }
                        goodsList.add(goods);
                }
                cursor1.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }

        try {
            Cursor cursor4 = sqldb.find("select THSL,TCKT_INX,INX,SP_ID from QTTHSL where ORDERNO =" + "'"+params.get("orderNo")+"'");
            MessageLog.writeLog("select THSL,TCKT_INX,INX,SP_ID from QTTHSL where ORDERNO =" + params.get("orderNo"));
            if (cursor4 != null && cursor4.getCount() > 0) {
                while (cursor4.moveToNext()) {
                    int Tckinx = CursorUtil.getInt(cursor4, "TCKT_INX");
                    int Inx = CursorUtil.getInt(cursor4, "INX");
                    int SPID = CursorUtil.getInt(cursor4, "SP_ID");
                    for (Goods goods : goodsList) {
                        if (goods.getTicktInx().equals(Tckinx + "") && goods.getTicktGoodsInx().equals(Inx + "") && goods.getId().equals(SPID + "")) {
                            goods.setHasedRefundCount(CursorUtil.getDouble(cursor4, "THSL") + "");
                        }
                    }
                }
                cursor4.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
     return goodsList;
    }

    public static void getGoodsFromCode(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){
        int SP_ID=-1000;
        int DEPTID=0;
        Goods goods = new Goods();
        try {



            Cursor cursor1 = sqldb.find("select BARCODE,SP_ID from SPBARCODE where BARCODE='"+params.get("barcode")+"'");
            MessageLog.writeLog("select BARCODE,SP_ID from SPBARCODE where BARCODE='"+params.get("barcode")+"'");
            if (cursor1 != null && cursor1.getCount() > 0) {
                while (cursor1.moveToNext()) {
                        SP_ID = CursorUtil.getInt(cursor1, "SP_ID");
                }
                if (SP_ID != -1000) {

                    Cursor cursor0 = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND R.RYDM="+"'"+params.get("personCode")+"'"+"  and (B.FDBH="+"'"+params.get("storeCode")+"'"+" or B.FDBH=-1) and R.STATUS=1");
                    if (cursor0 != null && cursor0.getCount() > 0) {
                        while (cursor0.moveToNext()) {
                            int id=CursorUtil.getInt(cursor0,"DEPTID");
                            Cursor cursor = sqldb.find("select W.DEPTID,B.BMDM from GTSP W,BM B where W.DEPTID=B.DEPTID and W.SP_ID ="+SP_ID+" and W.DEPTID="+id);
                            if (cursor != null && cursor.getCount() > 0) {
                                while (cursor.moveToNext()) {
                                    DEPTID = CursorUtil.getInt(cursor, "DEPTID");
                                    goods.setDeptId(DEPTID + "");
                                    goods.setDeptCode(CursorUtil.getString(cursor, "BMDM"));
                                    Cursor cursor2 = sqldb.find("select SP_ID,SPCODE,BARCODE,NAME,UNIT,SPGG,PACKED,szspbj,FKCXSBJ,SPTYPE,HSFS,LSDJ,HYLSDJ,STATUS,SPFL,SHOPID,SLYXS,FKCXSSJ from SPXX  where SP_ID=" + SP_ID + " and FDBH=" +"'"+ params.get("storeCode")+"'");
                                    if (cursor2 != null && cursor2.getCount() > 0) {
                                        while (cursor2.moveToNext()) {
                                            goods.setInputCode(CursorUtil.getString(cursor2, "SPCODE"));
                                            if(CursorUtil.getInt(cursor2,"SLYXS")==0){
                                                goods.setSaleCount("1.0");
                                            }else {
                                                goods.setSaleCount(CursorUtil.getInt(cursor2, "SLYXS") + "");
                                            }

                                            goods.setUnit(CursorUtil.getString(cursor2, "UNIT"));
                                            goods.setName(CursorUtil.getString(cursor2, "NAME"));
                                            goods.setId(SP_ID + "");
                                            goods.setCode(CursorUtil.getString(cursor2, "SPCODE"));
                                            goods.setBarCode(CursorUtil.getString(cursor2, "BARCODE"));
                                            goods.setBulkType(CursorUtil.getInt(cursor2, "SPTYPE") + "");
                                            goods.setCanSaleCount(CursorUtil.getInt(cursor2, "FKCXSSJ") + "");
                                            goods.setClassType(CursorUtil.getString(cursor2, "SPTYPE"));
                                            if("1".equals(goods.getClassType())&&!"null".equals(params.get("goodsPrice")+"")&&!"".equals(params.get("goodsPrice")+"")){
                                                goods.setGoodsGuid(CursorUtil.getMyUUID());
                                                goods.setPrice(params.get("goodsPrice")+"");
                                            }else if("1".equals(goods.getClassType())){
                                                if ("0.0".equals((CursorUtil.getDouble(cursor2, "LSDJ")) + "")) {
                                                    goods.setPrice("0");
                                                } else {
                                                    goods.setPrice(CursorUtil.getDouble(cursor2, "LSDJ") + "");
                                                }
                                            }else{
                                                if ("0.0".equals((CursorUtil.getDouble(cursor2, "LSDJ")) + "")) {
                                                    goods.setPrice("0");
                                                } else {
                                                    goods.setPrice(CursorUtil.getDouble(cursor2, "LSDJ") + "");
                                                }
                                                goods.setGoodsGuid(CursorUtil.getMyUUID());
                                            }
                                            goods.setDiscount("0.0");
                                            goods.setHasedRefundCount("0");
                                            goods.setImageUrl("");
                                            goods.setInx("0");
                                            goods.setMaxSaleCount(CursorUtil.getInt(cursor2, "FKCXSSJ") + "");
                                            goods.setPackaged(false);//PACKED
                                            goods.setPreferentialMoney("0.0");
                                            goods.setRefundCount("");
                                            goods.setSaleMoney((new BigDecimal(goods.getPrice()).multiply(new BigDecimal(goods.getSaleCount()))) + "");
                                            goods.setTicktGoodsInx(((int) ComSharedPreferences.getValueByName("INX", "goodsInx", 1, "") + 1) + "");
                                            ComSharedPreferences.WriteSharedPreferences("INX","goodsInx",Integer.parseInt(goods.getTicktGoodsInx()));//脱机商品序号
                                            goods.setTicktInx("0");

                                        }
                                        cursor2.close();
                                    } else {
                                        callback.onFailure("没有此商品！");
                                        return;
                                    }
                                }
                            }else{
                                callback.onFailure("此商品不能在本部门销售！");
                            }
                        }
                    }

                } else {
                    callback.onFailure("没有此商品！");
                    return;
                }
                cursor1.close();
            } else {
                callback.onFailure("没有此商品！");
                return;
            }
            if(goods.getGoodsGuid()==null||"".equals(goods.getGoodsGuid())){
                callback.onSuccess(CursorUtil.getOfflineBean(goods));
            }else {
                if (insertGoodOrder(sqldb, params, goods)) {
                    callback.onSuccess(CursorUtil.getOfflineBean(goods));
                } else {
                    callback.onFailure("商品查询失败！");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
    }


    public static Goods getGoodsFromGuid(Sqldb sqldb, Map<String, Object> params){
        try {
            Cursor cursor1 = sqldb.find("select * from ORDER_ITEM A,SPXX B where GUID='"+params.get("goodsGuid")+"' and A.SP_ID=B.SP_ID AND B.FDBH=" + "'"+params.get("storeCode") + "'"+" ORDER BY A.TCKT_INX,A.INX ASC");
            MessageLog.writeLog("select * from ORDER_ITEM A,SPXX B where GUID='"+params.get("goodsGuid")+"' and A.SP_ID=B.SP_ID AND B.FDBH=" + params.get("storeCode") + " ORDER BY A.TCKT_INX,A.INX ASC");
            if (cursor1 != null && cursor1.getCount() > 0) {
                while (cursor1.moveToNext()) {
                        Goods goods = new Goods();
                        goods.setInputCode(CursorUtil.getString(cursor1, "INPUTCODE"));
                        goods.setTicktInx(CursorUtil.getInt(cursor1, "TCKT_INX") + "");
                        goods.setTicktGoodsInx(CursorUtil.getInt(cursor1, "INX") + "");
                        goods.setDeptId(CursorUtil.getInt(cursor1, "DEPTID") + "");
                        goods.setDeptCode(CursorUtil.getString(cursor1, "DEPTCODE"));
                        goods.setId(CursorUtil.getInt(cursor1, "SP_ID") + "");
                        goods.setBarCode(CursorUtil.getString(cursor1, "BARCODE"));
                        goods.setSaleCount(params.get("count") + "");
                        goods.setPrice(params.get("goodsPrice") + "");
                        goods.setSaleMoney((new BigDecimal(params.get("goodsPrice") + "").multiply(new BigDecimal(params.get("count") + ""))) + "");
                        goods.setDiscount((CursorUtil.getDouble(cursor1, "ZKJE")) + "");
                        goods.setGoodsGuid(CursorUtil.getString(cursor1, "GUID"));
                        goods.setCode(CursorUtil.getString(cursor1, "CODE"));
                        goods.setPreferentialMoney((CursorUtil.getDouble(cursor1, "YHJE") - CursorUtil.getDouble(cursor1, "MBYH")) + "");
                        goods.setPersonId(CursorUtil.getInt(cursor1, "YYY") + "");
                        int SP_ID = CursorUtil.getInt(cursor1, "SP_ID");
                        Cursor cursor2 = sqldb.find("select SP_ID,SPCODE,BARCODE,NAME,UNIT,SPGG,PACKED,szspbj,FKCXSBJ,SPTYPE,HSFS,LSDJ,HYLSDJ,STATUS,SPFL,SHOPID,SLYXS,FKCXSSJ from SPXX  where SP_ID=" + SP_ID + " and FDBH=" + "'"+params.get("storeCode")+"'");
                        if (cursor2 != null && cursor2.getCount() > 0) {
                            while (cursor2.moveToNext()) {
                                goods.setName(CursorUtil.getString(cursor2, "NAME"));
                                goods.setUnit(CursorUtil.getString(cursor2, "UNIT"));
                                goods.setBulkType(CursorUtil.getInt(cursor2, "SPTYPE") + "");
                                goods.setClassType(CursorUtil.getString(cursor2, "SPTYPE"));
                                goods.setCanSaleCount(CursorUtil.getInt(cursor2, "FKCXSSJ") + "");
                            }
                            cursor2.close();
                        }
                        boolean isUpdate=false;
                        if(Double.parseDouble(params.get("count") + "")==0){
                            isUpdate= sqldb.delete("ORDER_ITEM","GUID=?",new String[]{params.get("goodsGuid")+""});
                            MessageLog.writeLog("ORDER_ITEM delete="+isUpdate);
                        }else {
                            ContentValues values = new ContentValues();
                            values.put("LSDJ", goods.getPrice());
                            values.put("XSSL", goods.getSaleCount());
                            values.put("XSJE", goods.getSaleMoney());
                             isUpdate = sqldb.update("ORDER_ITEM", values, "GUID=?", new String[]{params.get("goodsGuid") + ""});
                            MessageLog.writeLog("ORDER_ITEM update="+isUpdate+"/values="+new Gson().toJson(values));
                        }
                        if (isUpdate) {
                            return goods;
                        } else {
                            return null;
                        }
                }
                cursor1.close();
            }
        }catch (Exception e){
            e.printStackTrace();
            MessageLog.writeLog(e+"");
        }
        return null;
    }



    public static void frontDiscount(Sqldb sqldb, String disType, String money, Goods goods, String orderNo, OkHttpUtils.ResultCallback callback){
         if("0".equals(disType)){
             goods.setDiscount((new BigDecimal(goods.getPrice()).multiply(new BigDecimal(goods.getSaleCount())).multiply(new BigDecimal(money).divide(new BigDecimal(10))))+"");
         }else{
             goods.setDiscount(money);
         }
        ContentValues values = new ContentValues();
        values.put("ZKJE", goods.getDiscount());
        boolean isUpdate = sqldb.update("ORDER_ITEM", values, "GUID=?", new String[]{goods.getGoodsGuid()});
        MessageLog.writeLog("更新表 ORDER_ITEM ZKJE="+goods.getDiscount()+"isUpdate="+isUpdate);
        TicketInfo ticketInfo=new TicketInfo();
        List<SubTicketItem> subTickets=new ArrayList<>();
        SubTicketItem subTicketItem=new SubTicketItem();
        Map<String, Object> params=new HashMap<>();
        params.put("orderNo",orderNo);
        params.put("storeCode", ErpCommonData.storeCode);
        List<Goods> goodList=getGoodsList(sqldb,params);
        if(goodList!=null&&goodList.size()>0){
            for(int i=0;i<goodList.size();i++){
                if(goodList.get(i).getGoodsGuid().equals(goods.getGoodsGuid())){
                    goodList.get(i).setDiscount(goods.getDiscount());
                }
            }
        }
        subTicketItem.setGoodsList(goodList);
        subTickets.add(subTicketItem);
        ticketInfo.setSubTickets(subTickets);
        Cursor cursorMember = sqldb.find("select * from ORDER_MEM where ORDERNO="+orderNo);
        MessageLog.writeLog("select * from ORDER_MEM where ORDERNO="+orderNo);
        if(cursorMember!=null&&cursorMember.getCount()>0){
            while (cursorMember.moveToNext()){
                Member member=new Member();
                member.setMemberCardNo(CursorUtil.getString(cursorMember,"HYKNO"));
                member.setMemberCardTypeId(CursorUtil.getInt(cursorMember,"HYKTYPE"));
                ticketInfo.setMemberInfo(member);
            }
        }
        ContentValues values1 = new ContentValues();
        values1.put("ORDERNO",orderNo);
        values1.put("TCKT_INX",Integer.parseInt(goods.getTicktInx()));
        values1.put("INX", Integer.parseInt(goods.getTicktGoodsInx()));
        values1.put("ZKJE",new BigDecimal(goods.getDiscount())+"");
        values1.put("GUID", goods.getGoodsGuid());
        values1.put("ZKSL", new BigDecimal(goods.getSaleCount())+"");
        boolean isInsert1 = sqldb.insert("ORDER_ITEM_DISC", values1);
        MessageLog.writeLog("添加ORDER_ITEM_DISC表数据"+isInsert1+"/values1="+new Gson().toJson(values1));
        callback.onSuccess(CursorUtil.getOfflineBean(ticketInfo));
    }
}
